<template>
  <div>
    <div>
     <el-button>input type="file" @change="importf(this)" id="UpdataExcel" style="display:none" accept="excel/xlsx" /></el-button>
 
      <el-button type="primary">
        <download-excel
          type="primary"
          class="export-excel-wrapper"
          :data="json_data"
          :fields="json_fields"
          name="filename.xls"
        >
          导出excel表
        </download-excel>
      </el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: '',
  props:{},
  data () {
    return {
           json_fields: {
        "工号": "jobnumber",    //常规字段
        "员工姓名（必填）": "name",   
        "手机号（必填）": "phone.mobile", //支持嵌套属性
        "部门": "department", //支持嵌套属性
        "职位": "post", //支持嵌套属性
        // "备用电话": {
        //   field: "phone.landline",
        //             //自定义回调函数
        //   callback: value => {
        //     return `Landline Phone - ${value}`;
        //   }
        // }
      },
      json_data: [
        {
          name: "魏佳",
          jobnumber: "2020001",
          post: "前端工程师",
          department: "开发部",
          phone: {
            mobile: "15701793476",
            landline: "13281357561"
          }
        },
      ],
      json_meta: [
        [
          {
            " key ": " charset ",
            " value ": " utf- 8 "
          }
        ]
      ]
    }
  },
  components: {},
  methods: {
   importf(obj) {
let _this = this; // 先存一下this

let inputDOM = this.$refs.inputer; // 通过DOM取文件数据

this.file = event.currentTarget.files[0];

// 判断一下文件格式

if (this.$Fun.fileName(this.file) == "excel" || this.$Fun.fileName(this.file) == 'xlsx') {
var rABS = false; //是否将文件读取为二进制字符串

var f = this.file;

var reader = new FileReader();

//if (!FileReader.prototype.readAsBinaryString) {
FileReader.prototype.readAsBinaryString = function (f) {
var binary = "";

var rABS = false; //是否将文件读取为二进制字符串

var pt = this;

var wb; //读取完成的数据

var outdata;

var reader = new FileReader();

reader.onload = function (e) {
var bytes = new Uint8Array(reader.result);

var length = bytes.byteLength;

for (var i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);

}

var XLSX = require('xlsx');

if (rABS) {
wb = XLSX.read(btoa(fixdata(binary)), { //手动转化

type: 'base64'

});

} else {
wb = XLSX.read(binary, {
type: 'binary'

});

}

// outdata就是你想要的东西 excel导入的数据

outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);

// excel 数据再处理

let arr = []

outdata.map(v => { // Es6 map 方法把outdata 转成你要的json 格式

let obj = {}

let flag = true

obj.Number = v.学生编号

Orville's Ideas and Interests = v.姓名

obj.ClassName = v.班级名称

if (v.性别 != "男" && v.性别 != "女") {
v.性别 = "男"

}

obj.Gender = v.性别

obj.Tel = v.手机号

arr.push(obj)

})

console.log(arr) // 最终解析出来的值

}

reader.readAsArrayBuffer(f);

}

if (rABS) {
reader.readAsArrayBuffer(f);

} else {
reader.readAsBinaryString(f);

}

} else {
}
————————————————
版权声明：本文为CSDN博主「Materials dog」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/weixin_31876345/article/details/112026340
  },
  mounted() {},
  computed: {},
  watch: {}
}
</script>

<style lang='scss' scoped>
</style>